<?PHP

/*
 * 套系表
 */

/**
 * Description of series_product_model
 *
 * @author Jack.Y
 */
class Series_Model extends CI_Model {

    //重载CI_Model
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('Base_Model');
        $this->load->model('series_product_Model');
    }

    /*
     * Insert_Product
     * 写入数据库
     */

    function Insert_Product($data = FALSE) {
        $data['se_id'] = $this->Base_Model->GetID('se_id', 'db_series');
        $this->db->insert('db_series', $data);
        return $data['se_id'];
    }

    /*
     * GetForState
     * 获取列表数据
     * 参数：状态
     */

    function GetListForState($str = FALSE) {
        $this->db->order_by("se_id", "desc");
        $query = $this->db->get_where('db_series', array('se_state' => $str));
        //返回结果
        return $query->result_array();
    }

    /*
     * GetEntity
     * 获取单挑数据
     * 参数：se_id
     */

    function GetEntity($str = FALSE) {
        if ($str) {
            $query = $this->db->get_where('db_series', array('se_id' => $str));
            //返回结果
            return $query->result_array();
        } else {
            $Array = array(array("se_id" => '', "se_name" => '', "se_moeny" => '', "se_count" => ''));
            return $Array;
        }
    }

    /*
     * UpdateEntity
     * 更新单挑数据
     */

    function UpdateEntity($id = FALSE, $data = FALSE) {

        $this->db->where('se_id', $id);
        $result = $this->db->update('db_series', $data);
        //返回结果
        return $result;
    }

    /*
     * Insert_Product_Series
     * 分批写入Product和series_product
     */

    function Insert_Product_Series($data = FALSE, $array = FALSE) {
        $this->db->trans_start(); //开启事务
        $resultId = $this->Insert_Product($data);
        if ($array[0] != "") {
            $this->series_product_Model->Insert_Product_Series($array, $resultId);
        }
        $this->db->trans_complete();
    }

    /*
     * Update_Product_Series
     * 更新Product和series_product
     */

    function Update_Product_Series($id = FALSE, $data = FALSE, $array = FALSE) {
        $this->db->trans_start(); //开启事务
        $this->UpdateEntity($id, $data);
        $this->series_product_Model->DelDate($id); //删除原有数据
        if ($array[0] != "") {
            $this->series_product_Model->Insert_Product_Series($array, $id); //重新创建
        }
        $this->db->trans_complete();
    }

}

?>